package com.north.light.libpicselect.bean;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.north.light.libpicselect.PicSelParamsBuilder;
import com.north.light.libpicselect.utils.LibMediaSelectUtils;
import com.north.light.libpicselect.utils.LibPicToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * pic select adapter 选中的图片
 * <p>
 * 浏览的时候，设计成两个集合，是为了通用外部调用，外部调用传入List<String>即可，内部则需要转化
 */
public class LibPicSelIntentInfo {

    private static final String TAG = LibPicSelIntentInfo.class.getName();
    //浏览集合--控件内部显示数据
    private volatile List<LibPicInfo> mIntentList = new ArrayList<>();
    //传递需要浏览的图片
    private List<String> picList = new ArrayList<>();

    private static class SingleHolder {
        static LibPicSelIntentInfo mInstance = new LibPicSelIntentInfo();
    }

    public static LibPicSelIntentInfo getInstance() {
        return SingleHolder.mInstance;
    }

    public List<String> getPicList() {
        return picList;
    }

    public void setPicList(List<String> picList) {
        this.picList.clear();
        if (picList == null || picList.size() == 0) {
            return;
        }
        for (String pic : picList) {
            this.picList.add(pic);
        }
    }

    /**
     * 查询是否选中
     */
    public boolean isSel(String path) {
        try {
            for (LibPicInfo cache : mIntentList) {
                if (cache.getPath().equals(path)) {
                    return cache.isSelect();
                }
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 设置是否选中
     * 需要增加选择类型的判断
     */
    public void setSelPic(Context context,String path, int position, boolean isSel, int limit, PicSelParamsBuilder builder,
                          SelCountListener listener) {
        try {
            int selectCount = 0;
            for (LibPicInfo cache : mIntentList) {
                if (cache.isSelect()) {
                    selectCount++;
                }
            }
            if (selectCount >= limit && isSel) {
                if (listener != null) {
                    listener.limit();
                    return;
                }
            }
            //判断是否符合条件--------------------------------------------
            if (isSel) {
                boolean conditionRes = LibMediaSelectUtils.checkCondition(
                        this.mIntentList.get(position), getSelectInfo(), builder, new LibMediaSelectUtils.TipsListener() {
                            @Override
                            public void message(String tips) {
                                LibPicToastUtils.toast(context.getApplicationContext(), tips, Toast.LENGTH_SHORT);
                            }
                        });
                if (!conditionRes) {
                    if (listener != null) {
                        listener.noPass();
                    }
                    return;
                }
            }
            if (this.mIntentList.get(position).getPath().equals(path)) {
                this.mIntentList.get(position).setSelect(isSel);
                if (listener != null) {
                    listener.selCount(isSel ? ++selectCount : --selectCount);
                }
            } else {
                this.mIntentList.get(position).setSelect(false);
            }
        } catch (Exception e) {
            Log.d(TAG, "sel pic error: " + e.getMessage());
        }
    }

    /**
     * 获取选中的数据集合
     */
    public List<LibPicInfo> getSelectInfo() {
        if (this.mIntentList == null || mIntentList.size() == 0) {
            return new ArrayList<>();
        }
        List<LibPicInfo> result = new ArrayList<>();
        for (LibPicInfo cache : mIntentList) {
            if (cache.isSelect()) {
                result.add(cache);
            }
        }
        return result;
    }


    /**
     * 设置数据
     */
    public void setPicSelList(List<LibPicInfo> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.mIntentList = list;
    }

    /**
     * 获取选择集合
     */
    public int selCount() {
        int selectCount = 0;
        for (LibPicInfo cache : mIntentList) {
            if (cache.isSelect()) {
                selectCount++;
            }
        }
        return selectCount;
    }

    /**
     * 获取图片集合
     */
    public List<LibPicInfo> getPicSelList() {
        return mIntentList;
    }

    /**
     * 接口
     */
    public interface SelCountListener {
        void selCount(int count);

        void limit();

        void noPass();
    }
}
